﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lyh.Dbdg.Business;
using Lyh.Dbdg.Model;

namespace Lyh.Dbdg.UI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            LoadCategory();
        }

        private void LoadCategory()
        {
            cbCategory.DataSource = Enum.GetNames(typeof(Category));
        }

        private void LoadTables()
        {
            clbTables.DataSource = DatabaseManager.GetTables();
            clbTables.ValueMember = "Name";
            clbTables.DisplayMember = "Name";
        }

        private void InitParameters()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("server", txtServer.Text.Trim());
            dic.Add("database", txtDatabase.Text.Trim());
            dic.Add("uid", txtUid.Text.Trim());
            dic.Add("pwd", txtPwd.Text.Trim());
            Context context = new Context(cbCategory.Text);
            context.Setting = dic;
            DatabaseManager.RefreshConfig((Category)Enum.Parse(typeof(Category), cbCategory.Text));
        }

        private Database GetDatabaseStructure()
        {
            var db = DatabaseManager.GetCurrentDatabase();
            foreach (var item in clbTables.CheckedItems)
            {
                var table = item as Table;
                table.Columns = DatabaseManager.GetColumns(table.Name);
                db.Tables.Add(table);
            }
            return db;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtDatabase.Text))
                {
                    MessageBox.Show("请输入数据库名");
                    return;
                }
                InitParameters();
                LoadTables();
                btnCreate.Enabled = true;
                MessageBox.Show("连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnChooseFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            DocumentBuilder.Save(txtFolder.Text.Trim(), GetDatabaseStructure());
            MessageBox.Show("生成成功");
        }

        private void lnkAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < clbTables.Items.Count; i++)
            {
                clbTables.SetItemChecked(i, true);
            }
        }

        private void lnkReserve_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < clbTables.Items.Count; i++)
            {
                clbTables.SetItemChecked(i, !clbTables.GetItemChecked(i));
            }
        }
    }
}
